﻿using System;
using TheWorldLibrary.GameStates;
using Microsoft.Xna.Framework;
using TheWorldLibrary.Controls;
using TheWorld.Global;
using Microsoft.Xna.Framework.Content;
using TheWorld.Networking;

namespace TheWorld.GameScreens
{
    public class ChooseCharacterScreen : BaseGameState
    {
        #region Field Region

        Button useCharacter1;
        Button useCharacter2;
        Button useCharacter3;

        public int[] spriteNum = new int[3];

        PictureBox sp1;
        PictureBox sp2;
        PictureBox sp3;

        public string[] names = new string[3];

        #endregion

        #region Property Region

        #endregion

        #region Constructor Region

        public ChooseCharacterScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method Region

        protected override void LoadContent()
        {
            ContentManager Content = GameRef.Content;

            base.LoadContent();

            CreateControls();

        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Method Region

        void CreateControls()
        {
            if (spriteNum[0] != 0)
            {
                sp1 = new PictureBox(GfxManager.Instance.Sprites[spriteNum[0]], new Rectangle(375, 425, 32, 32), new Rectangle(0, 0, 32, 32));
                if (sp1 != null)
                {
                    ControlManager.Add(sp1);

                    useCharacter1 = new Button(GfxManager.Instance.Buttons[15], GfxManager.Instance.Buttons_C[15], GfxManager.Instance.Buttons_C[15], new Vector2(300, 500), true);
                    useCharacter1.Clicked += new EventHandler(useCharacter1_Clicked);
                    ControlManager.Add(useCharacter1);
                }
            }

            if (spriteNum[1] != 0)
            {
                sp2 = new PictureBox(GfxManager.Instance.Sprites[spriteNum[1]], new Rectangle(545, 425, 32, 32), new Rectangle(0, 0, 32, 32));
                if (sp2 != null)
                {
                    ControlManager.Add(sp2);

                    useCharacter2 = new Button(GfxManager.Instance.Buttons[15], GfxManager.Instance.Buttons_C[15], GfxManager.Instance.Buttons_C[15], new Vector2(470, 500), true);
                    useCharacter2.Clicked += new EventHandler(useCharacter2_Clicked);
                    ControlManager.Add(useCharacter2);
                }
                else
                {
                    useCharacter2 = new Button(GfxManager.Instance.Buttons[15], GfxManager.Instance.Buttons_C[15], GfxManager.Instance.Buttons_C[15], new Vector2(470, 500), true);
                    useCharacter2.Clicked += new EventHandler(createChar_Clicked);
                    ControlManager.Add(useCharacter2);
                }
            }

            if (spriteNum[2] != 0)
            {
                sp3 = new PictureBox(GfxManager.Instance.Sprites[spriteNum[2]], new Rectangle(715, 425, 32, 32), new Rectangle(0, 0, 32, 32));
                if (sp3 != null)
                {
                    ControlManager.Add(sp3);

                    useCharacter3 = new Button(GfxManager.Instance.Buttons[15], GfxManager.Instance.Buttons_C[15], GfxManager.Instance.Buttons_C[15], new Vector2(640, 500), true);
                    useCharacter3.Clicked += new EventHandler(useCharacter3_Clicked);
                    ControlManager.Add(useCharacter3);
                }
                else
                {
                    useCharacter3 = new Button(GfxManager.Instance.Buttons[15], GfxManager.Instance.Buttons_C[15], GfxManager.Instance.Buttons_C[15], new Vector2(640, 500), true);
                    useCharacter3.Clicked += new EventHandler(createChar_Clicked);
                    ControlManager.Add(useCharacter3);
                }
            }
        }

        void useCharacter3_Clicked(object sender, EventArgs e)
        {
            ClientTcp.Instance.SendUseChar(names[2]);
        }

        void useCharacter2_Clicked(object sender, EventArgs e)
        {
            ClientTcp.Instance.SendUseChar(names[1]);
        }

        void useCharacter1_Clicked(object sender, EventArgs e)
        {
            ClientTcp.Instance.SendUseChar(names[0]);
        }

        void createChar_Clicked(object sender, EventArgs e)
        {
            Transition(ChangeType.Change, Client.CreateCharacterScreen);
        }

        #endregion
    }
}
